package com.fire.control.system.module.index.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fire.control.system.dto.UserDTO;
import com.fire.control.system.dto.app.AppNoticeVo;
import com.fire.control.system.dto.app.AppRespWarningVo;
import com.fire.control.system.dto.app.AppUserVo;
import com.fire.control.system.module.index.service.AppIndexService;
import com.fire.control.system.module.notice.dao.NoticeMapper;
import com.fire.control.system.module.user.dao.UserMapper;
import com.fire.control.system.module.warning.dao.WarningMapper;
import com.fire.control.system.statics.ResponseMessage;
import com.fire.control.system.support.protocol.app.AppRequestProtocol;
import com.fire.control.system.support.protocol.comman.ProtocolSelector;
import com.fire.control.system.support.protocol.comman.ResponseProtocol;
import com.fire.control.system.support.util.XdaoDate;

/**
 * 
 * @ClassName:  AppIndexServiceImpl
 * @Description:  App首页服务层接口具体实现
 * @author:  xiechengguang
 * @date:  2017年6月8日 上午9:51:08
 *
 */
@Service
public class AppIndexServiceImpl implements AppIndexService {
	@Autowired private ProtocolSelector protocolSelector;
	@Autowired private WarningMapper warningMapper;
	@Autowired private UserMapper userMapper;
	@Autowired private NoticeMapper noticeMapper;
	
	
	/**
	 * <p>Title: initData</p> 
	 * <p>Description: 获取首页数据</p> 
	 * @param requestProtocol
	 * @return 
	 * @see com.fire.control.system.AppIndexService.service.IndexService#initData(com.fire.control.system.support.protocol.app.AppRequestProtocol)
	 */
	@Override
	public ResponseProtocol initData(AppRequestProtocol requestProtocol,UserDTO u) {
		ResponseProtocol response = protocolSelector.choose(requestProtocol);
			
		// 用户信息
		AppUserVo user = new AppUserVo();
		user.set(u);
		
		// 最新一条消息
		List<String> notices = noticeMapper.getTitles(u.getUserId());
		// 火灾报警
		List<AppRespWarningVo> fires = warningMapper.fireWarnings(u.getUserId());
		// 故障报警
		List<AppRespWarningVo> faults = warningMapper.deviceFault(u.getUserId());
		
		fires.addAll(faults); //将火灾报警和故障报警放在同一个list下
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("user", user);
		map.put("notices", notices);
		map.put("alarm", fires);
		
		ResponseMessage.CODE_200.messageToProtocol(response);
		response.setDate(new XdaoDate());
		response.setData(map);
		return response;
		
	}
	
	

}
